Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Local list foreach value of list + foreach var in { in the same loop

    Hi Statalista, please, could anyone help me with my loop code?

    I have 10 .dta. Each one is called file1, file2...file10.
    I need to run commands creating new variables in each of those files.
    I am trying to do the following but it is not working.

    Thank you!!

    Code:
    local list 1 2 3 4 5 6 7 8 9 10
    foreach value of local list{
    use file`var' , clear
    
    foreach i in x1 x2 x3 {
    gen sum_`i' = 'i`/weight
    gen partial_`i' = 1/weight
    gen total_`'i = sum_`i'/partial_`i'
    
    save new file_`var'
    
    }
    }

  • #2
    use -forvalues- instead,
    Code:
    forvalues i = 1/10 {
    use file`i', clear
    
    foreach v in x1 x2 x3 {
    gen sum_`v' = `v'/weight
    gen partial_`v' = 1/weight  
    gen total_`v' = sum_`v'/partial_`v'
    }
    
    save newfile`i', replace
    }

    Comment


    • #3
      The problems of #1 start with

      Declaring a loop over local macro value -- except that you never use it inside the loop, so the same code is just repeated. That is not illegal, but it is not what you want.

      Mentioning a local macro var -- which is never defined in the code you show us. That is not illegal but your code will fail on

      Code:
      use file, clear
      if there is no such file.

      Otherwise put, you say value in one place and var in others, but your purpose requires that you use the same name.


      "not working" is never a good report. See 12.1 at https://www.statalist.org/forums/help#stata

      What did happen that you didn't want?

      What didn't happen that you did want?

      Did nothing happen?

      Did you get an error message? If so, what was it?

      Comment


      • #4
        In addition to the problems pointed out by Oyvind and Nick, the inner loop contains another syntax error, and when that is correct, there appears to be no point to even having it:
        foreach i in x1 x2 x3 {
        gen sum_`i' = 'i`/weight
        gen partial_`i' = 1/weight
        gen total_`'i = sum_`i'/partial_`i'
        }
        The quotes on the part that is highlighted in red are reversed and will produce a syntax error. You can fix that easily enough.

        Now let's consider what the loop actually does. Start with the first iteration: `i' is x1. So we create sum_x`1 = x1/weight. Then we generate partial_`x1' = 1/weight. On the third line we create total_x1 = sum_x1/partial_x1. But since sum_x1 is x1/weight, and partial_x1 is 1/weight, total_x1 will be (x1/weight)/(1/weight) which is just x1 (unless weight = 0, in which case it is missing value). Since total_x1 is just x1 itself, there seems to be no point in creating it. The same things happen with x2 and x3: you just get the original variable back (except if weight = 0, in which case you get missing value.) Moreover, there is no point in creating three separate variables partial_x1, partial_x2, and partial_x3 which are all equal to 1/weight.

        If these are really the results you intend from the loop, you can eliminate the loop altogether and just -gen partial = 1/weight-. I suspect, however, that this is not what you really have in mind. So in addition to getting the errors out so that it can run, you need to reconsider the content of the loop itself so that it will do whatever it is you really want.

        Comment

        Working...
        X